const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showAdvert:false,
    dates:5,
    advert:{}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    checkEject(){
      let that=this;
      let pages = getCurrentPages(); //获取加载的页面
      let currentPage = pages[pages.length - 1]; //获取当前页面的对象
      let url = currentPage.route ;//当前页面url
      let user = app.functions.getLocalStorage("user");
      let shop = app.functions.getLocalStorage("shop");
      if(app.functions.isEmpty(user))return;
      let params={
        centreId:shop.centreId,
        memberId:user.id
      };
      app.api.checkEject(params,(response)=>{
        if(app.functions.isEmpty(response.data))return;
        if(response.code==0){
          that.setData({
            advert:response.data,
            dates:5,
            showAdvert:true
          })
          clearInterval(that.timer);
            that.data.timer = setInterval(()=>{
              if (that.data.dates == 1) {
                that.setData({
                  showAdvert:false
                })
                clearInterval(that.data.timer);
                // 关闭再次弹窗
                that.hd_checkadd();
                // 弹券
                app.cache[url].showFrame();
              } else {
                that.setData({
                  dates:that.data.dates-1
                })
              }
            }, 1000);
        }else{
          // 弹券
          app.cache[url].showFrame();
        }
      },(error)=>{

      })
    },
    // 关闭弹窗
    hd_checkadd(){
      let that=this;
      let user = app.functions.getLocalStorage("user");
      let shop = app.functions.getLocalStorage("shop");
      let params={
        memberId:user.id,
        centreId:shop.centreId,
        state:that.data.advert.typeId,
        type:that.data.advert.appUrl
      };
      app.api.hdCheckAdd(params,(response)=>{

      },(error)=>{

      })
    },
    // 点击广告
    advertTab() {
      let pages = getCurrentPages(); //获取加载的页面
      let currentPage = pages[pages.length - 1]; //获取当前页面的对象
      let url = currentPage.route ;//当前页面url
      let that=this;
      let selecthd=app.cache[url].data.selecthd;
      that.setData({
        dates:5,
        showAdvert:false
      },()=>{
        clearInterval(that.data.timer);
      })
      that.hd_checkadd();
      let adinfo = that.data.advert;
      if (adinfo.typeId == 3){
        let adindex;
        selecthd.forEach(function(item, index, array) {
          if (item.id == adinfo.appUrl){
            adindex = index;
          }
        });
        let active = 2 + parseInt(adindex);
        app.cache[url].selecthdcheck(active,adinfo);
      } else {
        wx.navigateTo({
            url:"/"+adinfo.appUrl
        })
      }
    },
  }
})
